//
//  LRJBaseDemoViewController.m
//  LRJAansyDraw_Example
//
//  Created by imac_LRJ on 2022/6/27.
//  Copyright © 2022 imac_LRJ. All rights reserved.
//

#import "LRJBaseDemoViewController.h"

#import "LRJ_SampleCoreTextView.h"
#import "UIImage+Graver.h"

#import "LRJ_TextParagrapStyle.h"
#import "NSMutableAttributedString+advanceMethods.h"
//
#import "NSAttributedString+drawAndCalculate.h"
#import "LRJ_CTextLayout.h"
#import "LRJBaseTableViewCell.h"

@interface LRJBaseDemoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) LRJ_SampleCoreTextView *myView;

@property (weak, nonatomic) IBOutlet UITableView *demoTableView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@end

@implementation LRJBaseDemoViewController

- (void)testTable
{
    self.demoTableView.delegate = self;
    self.demoTableView.dataSource = self;
    self.dataArr = [NSMutableArray array];
    for (int i = 0; i < 10; i++) {
        LRJ_MutableAttributedItem *itme1 = [[LRJ_MutableAttributedItem alloc] initWithText:@"异步绘制的基础demo"];
        [itme1 setFont:[UIFont systemFontOfSize:18]];
        [itme1 setColor:[UIColor redColor]];
        
        LRJ_MutableAttributedItem *itme2 = [[LRJ_MutableAttributedItem alloc] initWithText:@"点击相应区域"];
    //    [itme2 addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
        [itme2 setFont:[UIFont systemFontOfSize:18]];
        [itme2 setColor:[UIColor orangeColor]];
        
        [itme1 appendAttributedItem:itme2];
        [itme1 appendWhiteSpaceWithWidth:20];
        
        
        //添加标签
        CGSize descSize = [itme2.resultString wmg_sizeConstrainedToWidth:1000 numberOfLines:1];
        
        
        UIImage *contentImage = [[UIImage wmg_imageWithColor:[UIColor blueColor] size:CGSizeMake(descSize.width + 8, descSize.height)] wmg_roundedImageWithCornerRadius:5 cornerType:UIRectCornerTopLeft | UIRectCornerBottomLeft];
        contentImage = [contentImage wmg_drawItem:itme2 numberOfLines:1 atPosition:CGPointMake(4, 0)];
        [itme1 appendImageWithImage:contentImage size:CGSizeMake(descSize.width + 8, descSize.height)];
        
        //添加图片 并添加空隙
        for (int i = 0; i < 10; i++) {
            [itme1 appendWhiteSpaceWithWidth:10];
            [itme1 appendImageWithImage:[UIImage imageNamed:@"printing_s"] size:CGSizeMake(18, 18)];
        }
        
        LRJ_MutableAttributedItem *itme3 = [[LRJ_MutableAttributedItem alloc] initWithText:@"测试技术的冷风机拉数据东方丽景市解放路看三大件，看看那能不能展开哈哈哈哈哈哈"];
    //    [itme3 addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
        [itme3 setFont:[UIFont systemFontOfSize:18]];
        [itme3 setColor:[UIColor cyanColor]];
        
        
        [itme1 appendAttributedItem:itme3];
        
        //添加段落样式
        LRJ_TextParagrapStyle *style = [LRJ_TextParagrapStyle defaultParagraphStyle];
        style.lineBreakMode =  NSLineBreakByCharWrapping  ;       // Wrap at character boundaries
        style.lineSpacing = 20;
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"展开"];
        [str wmg_setFont:[UIFont systemFontOfSize:22]];
        [str wmg_setColor:[UIColor orangeColor]];
        [itme1 setTextParagraphStyle:style];
        CGSize size = [itme1.resultString wmg_sizeConstrainedToWidth:[UIScreen mainScreen].bounds.size.width - 20 numberOfLines:3];
        itme1.height = size.height;
        [self.dataArr addObject:itme1];
    }
        
    
    
}


- (void)baseDemo
{
    LRJ_SampleCoreTextView *viewBase =  [LRJ_SampleCoreTextView new];
    LRJ_MutableAttributedItem *itme1 = [[LRJ_MutableAttributedItem alloc] initWithText:@"异步绘制的基础demo"];
    [itme1 setFont:[UIFont systemFontOfSize:18]];
    [itme1 setColor:[UIColor redColor]];
    
    LRJ_MutableAttributedItem *itme2 = [[LRJ_MutableAttributedItem alloc] initWithText:@"点击相应区域"];
//    [itme2 addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
    [itme2 setFont:[UIFont systemFontOfSize:18]];
    [itme2 setColor:[UIColor orangeColor]];
    
    [itme1 appendAttributedItem:itme2];
    [itme1 appendWhiteSpaceWithWidth:20];
    
    
    //添加标签
    CGSize descSize = [itme2.resultString wmg_sizeConstrainedToWidth:1000 numberOfLines:1];
    
    
    UIImage *contentImage = [[UIImage wmg_imageWithColor:[UIColor blueColor] size:CGSizeMake(descSize.width + 8, descSize.height)] wmg_roundedImageWithCornerRadius:5 cornerType:UIRectCornerTopLeft | UIRectCornerBottomLeft];
    contentImage = [contentImage wmg_drawItem:itme2 numberOfLines:1 atPosition:CGPointMake(4, 0)];
    [itme1 appendImageWithImage:contentImage size:CGSizeMake(descSize.width + 8, descSize.height)];
    
    //添加图片 并添加空隙
    for (int i = 0; i < 10; i++) {
        [itme1 appendWhiteSpaceWithWidth:10];
        [itme1 appendImageWithImage:[UIImage imageNamed:@"printing_s"] size:CGSizeMake(18, 18)];
    }
    
    LRJ_MutableAttributedItem *itme3 = [[LRJ_MutableAttributedItem alloc] initWithText:@"测试技术的冷风机拉数据东方丽景市解放路看三大件，看看那能不能展开哈哈哈哈哈哈"];
//    [itme3 addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
    [itme3 setFont:[UIFont systemFontOfSize:18]];
    [itme3 setColor:[UIColor cyanColor]];
    
    
    [itme1 appendAttributedItem:itme3];
    
    //添加段落样式
    LRJ_TextParagrapStyle *style = [LRJ_TextParagrapStyle defaultParagraphStyle];
    style.lineBreakMode =  NSLineBreakByCharWrapping  ;       // Wrap at character boundaries
    style.lineSpacing = 20;
    viewBase.numerOfLines = 3;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"展开"];
    [str wmg_setFont:[UIFont systemFontOfSize:20]];
    [str wmg_setColor:[UIColor orangeColor]];
    viewBase.textDrawer.textLayout.truncationString = str;
    [itme1 setTextParagraphStyle:style];
    CGSize size = [itme1.resultString wmg_sizeConstrainedToWidth:[UIScreen mainScreen].bounds.size.width - 20 numberOfLines:3];
    viewBase.attributedItem = itme1;
    viewBase.backgroundColor = [UIColor blackColor];
    __weak typeof(viewBase) weakViewBase = viewBase;
    viewBase.trancatedBlock = ^{
        weakViewBase.numerOfLines = 0;
        CGSize size = [itme1.resultString wmg_sizeConstrainedToWidth:[UIScreen mainScreen].bounds.size.width - 20 numberOfLines:0];
        weakViewBase.frame = CGRectMake(10, 10, size.width, size.height);
        [weakViewBase redraw];
    };
    
    
    
    viewBase.frame = CGRectMake(10, 10, size.width, size.height + 5);
    [self.view addSubview:viewBase];
    self.myView = viewBase;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.style == baseStyle) {
        [self baseDemo];
    } else {
        [self testTable];
    }
    
}

- (void)pushAction:(id)sender
{
//    LRJ_MutableAttributedItem *item = sender;
//    NSLog(@"%@", item.resultString.string);
//    
//    self.myView.numerOfLines = 0;
//    [self.myView redraw];
    
}
#pragma mark --- tableview代理

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    LRJBaseTableViewCell
    LRJ_MutableAttributedItem *itme1  = self.dataArr[indexPath.row];
    itme1.indexP = indexPath;
    LRJBaseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LRJBaseTableViewCell class])];
    if (!cell) {
        cell = [[LRJBaseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([LRJBaseTableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.weakTab = tableView;
    [cell setData:itme1];
    return cell;
    return nil;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRJ_MutableAttributedItem *itme1  = self.dataArr[indexPath.row];
    
    return itme1.height + 30;
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
